<?php


namespace console\spider;


use console\models\School;
use console\spider\utils\HtmlQuery;
use console\spider\utils\HttpClient;

class SchoolSpider
{

    //爬取学校详细信息
    public static function getSchoolInfo($school)
    {

        try {
            $data =  HttpClient::request(Params::schoolInfoUrl.$school->entity->zzXxJbxx->id);

            if($data['code'] == 200)
            {
                $schoolInfo = HtmlQuery::getData($data['content']);
                $model = School::findOne([
                    'ZZXXID' => $school->entity->zzXxJbxx->id,
                ]);

                if(empty($model))
                    $model = new School();

                $model->ZZXXID = $school->entity->zzXxJbxx->id;
                $model->DM = trim($schoolInfo[0][3]);
                $model->ZWMC = $school->entity->zzXxJbxx->xxjgmc;
                $model->YWMC = trim($schoolInfo[2][3]);
                //地址
                $model->DZ = trim($schoolInfo[3][3]);
                $DZDM = trim($schoolInfo[4][3]);
                $model->DZDM = empty($DZDM)?null:$DZDM;
                $model->JC = '';
                $model->CS = $school->entity->zzXxJbxx->xxszsmc;//城市
                $model->QX = $school->entity->zzXxJbxx->xxszxmc;;//区/县
                ////属地教育部门代码
                $model->SDJYBMDM = trim($schoolInfo[5][3]);
                //属地教育部门名称
                $model->SDJYBMMC = trim($schoolInfo[6][3]);
                //行政区划码
                $model->XZQHM = trim($schoolInfo[7][3]);
                //驻地城乡类型
                $model->ZDCXLX = trim($schoolInfo[7][7]);
                //举办者名称
                $model->JBZMC =trim($schoolInfo[8][3]);
                //办学类别
                $model->BXLB =  trim($schoolInfo[9][3]);
                //独立设置少数民族学校
                $model->SSMZXX = trim($schoolInfo[9][7])=="是"? 1:0;
                //学校类别
                $model->XXLB = trim($schoolInfo[10][3]);
                //附设班类型
                $model->FSBLX = trim($schoolInfo[10][7]);
                //法人证书号
                $model->FRZSH = trim($schoolInfo[11][3]);
                //法人代表名称
                $model->FRDBMC = trim($schoolInfo[11][7]);
                //负责人姓名
                $model->FZRXM = trim($schoolInfo[12][3]);
                //负责人联系电话
                $model->FZRLXDH =trim($schoolInfo[12][7]);
                //建校年月
                $model->JXNY = empty(trim($schoolInfo[13][3]))?null:trim($schoolInfo[13][3])."01";
                //联系电话
                $model->LXDH = trim($schoolInfo[13][7]);
                //传真电话
                $model->CZDH   =   trim($schoolInfo[14][3]);
                $model->ZYDZ   = trim($schoolInfo[14][7]);//主页地址
                $model->YZBM   =  trim($schoolInfo[15][3]);//邮政编码
                $model->DZXX   = trim($schoolInfo[16][3]);//电子信箱
                $model->PGLX   = trim($schoolInfo[16][7]);//评估类型
                $model->SFJLXYW = trim($schoolInfo[17][3])=="是"? 1:0;//是否建立校园网
                $model->ZGLB   = trim($schoolInfo[19][3]);//主管类别
                $model->XXBB   = trim($schoolInfo[20][3]);//学校办别
                $model->SZDQLB = trim($schoolInfo[20][7]);//所在地区类别
                $model->XZXM   = trim($schoolInfo[21][3]);//校长姓名
                $model->XZSJH  =  trim($schoolInfo[21][7]);//校长手机号码
                $model->XJLXR  = trim($schoolInfo[22][3]);//学籍联系人
                $model->XJLXDH = trim($schoolInfo[22][7]);//学籍联系电话
                $model->ZSZXDH = trim($schoolInfo[23][3]); //招生咨询电话
                $model->ZSZXYX = trim($schoolInfo[23][7]);//招生咨询邮箱
                $model->JJ = " ";//简介
                $model->ZT = " ";//状态
                if($model->save())
                    echo '.';
                else
                {
                    echo $model->ZWMC.": error\n";
                    var_export($model);
                }
            }
        }catch (Exception $e) {
            echo $e;
        }
    }

    /**
     * 抓取学校列表
     */
    public static function getSchools()
    {
        $params = Array();
        $params['dir'] = Params::schoolDir;
        $url = Params::schoolsUrl;
        $params['fileName'] = "school";
        $params['tag']= "学校列表";
        HttpClient::dataSave($url,$params);
    }

}